/**
 *@描述: 确认框
 *@date:2022/6/6
 *@author:heli
 */
import React from "react";
import ReactDOM from 'react-dom';
import Dialog from "../Dialog";

interface PropsRule {
    title?: string
    children?: React.ReactNode | string,
}

export default function ({children, title}: PropsRule): any {
    return new Promise((resolve, reject) => {
        const visible = true;
        let div = document.createElement('div');
        document.body.appendChild(div);

        function destory() {
            let timeout;
            timeout = setTimeout(() => {
                ReactDOM.unmountComponentAtNode(div);
                document.body.removeChild(div);
                clearTimeout(timeout);
                timeout = null;
            }, 1500);
        }

        function handleOk() {
            destory();
            return resolve(true);
        }

        function handleClose() {
            destory();
            return reject(false);
        }

        ReactDOM.render(
            <Dialog title={title} visible={visible} confirm={handleOk} cancel={handleClose}>
                {children}
            </Dialog>,
            div
        );
    });
}
